﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using eDoc.BO;

namespace eDoc.Controls.App
{
    public partial class ucListApp : System.Web.UI.UserControl
    {
        private BO.Sescurity _security = new Sescurity();
        private string username = "";
        private int AppMode = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (_security.CheckLogin(HttpContext.Current))
            {
                //if (Session["Portal_Username"] != null)
                //    username = Session["Portal_Username"].ToString();
                //if (Session["Portal_AppMode"] != null)
                //    AppMode = Convert.ToInt32(Session["Portal_AppMode"].ToString());

                username = _security.CurrentUser(HttpContext.Current).Username;
                AppMode = _security.CurrentUser(HttpContext.Current).AppMode;
                
            }
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            
            DataTable dt = new DataTable();

            if (username.ToLower() == Sescurity.UserRoleAdmin)
            {
                dt = Data.Select_AppPlist();
                
            }
            else
            {
                dt = Data.Select_AppPlist_By_AppMode(AppMode);
            }

            grdAppList.DataSource = dt;
            grdAppList.DataBind();
        }

        private void BindDataPagging()
        {
            string strSelect = ddlSelect.SelectedValue;
            DataTable dt = new DataTable();
            if (AppMode == 0)
            {
                if (strSelect == "0")
                {
                    dt = BO.Data.Select_AppPlist();
                }
                else if (strSelect == "1")
                {
                    dt = BO.Data.Select_AppPlist_JB(false, true);
                }
                else
                {
                    dt = BO.Data.Select_AppPlist_JB(true, true);
                }
            }
            else
            {
                if (strSelect == "0")
                {
                    dt = Data.Select_AppPlist_By_AppMode(AppMode);
                }
                else if (strSelect == "1")
                {
                    dt = BO.Data.Select_AppPlist_JB(false, true, AppMode);
                }
                else
                {
                    dt = BO.Data.Select_AppPlist_JB(true, true, AppMode);
                }
            }

            grdAppList.DataSource = dt;
            grdAppList.DataBind();
        }

        protected void ddlSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindDataPagging();
        }

        protected string ReturnPublished(string bl)
        {
            if (Convert.ToBoolean(bl))
                return "Published";
            else
                return "Testing";
        }

        protected string ReturnHardware(string str)
        {
            string value = "";
            if(str.Contains("1"))
            {
                value="iPhone";
            }

            if (str.Contains("2"))
            {
                if (value == "")
                {
                    value = "iPad";
                }
                else
                {
                    value += ", iPad";
                }

            }
                return value;
        }

        protected string ReturnDownload(string bl, string id)
        {
            //<a title="Download" class="iconDown info-tooltip iframe" href="/logindownload.aspx?id=<%# Eval("ID") %>"></a>
            string value = "<a title='Download' class='iconDown info-tooltip' href='/logindownload.aspx?id=" + id + "'></a>";

            if (!Convert.ToBoolean(bl))
                value = "";

            return value;
        }

        protected string ReturnJB(string jb)
        {
            
            string value = "<img src='/images/iconJB.png' />";

            if (!Convert.ToBoolean(jb))
                value = "<img src='/images/iconApple.png' />";

            return value;
        }


        protected void grdAppList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdAppList.PageIndex = e.NewPageIndex;
            BindDataPagging();
            //BindData();
        }

        protected string ReturnAppIcon(string appIcon)
        {
            if (string.IsNullOrEmpty(appIcon))
            {
                return "/images/apple-02.png";
            }
            else
            {
                return appIcon;
            }

        }

        protected string ReturnExpirationDate(string expirationDate)
        {
            if(string.IsNullOrEmpty(expirationDate))
            {
                return "";
            }
            else
            {
                return Convert.ToDateTime(expirationDate).ToString("dd/MM/yyyy HH:mm");
            }
        }

       
    }
}